package com.dpp.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class GoogleBarCodeUtils {
    /**
     * 条形码宽度
     */
    private static final int WIDTH = 603;

    /**
     * 条形码高度
     */
    private static final int HEIGHT = 120;

    /**
     * 加文字 条形码
     */
    private static final int WORDHEIGHT = 183;
    /**
     * 右上角文字
     */
    private static final String RIGHT_UP_WORDS = "代跑跑吧";
    /**
     * 设置 条形码参数
     */
    private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
        private static final long serialVersionUID = 1L;

        {
            // 设置编码方式
            put(EncodeHintType.CHARACTER_SET, "utf-8");
            //容错级别 这里选用最高级H级别
            put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            put(EncodeHintType.MARGIN, 0);

        }
    };

    /**
     * 生成 图片缓冲
     *
     * @param vaNumber VA 码
     * @return 返回BufferedImage
     * @author myc
     */
    public static BufferedImage getBarCode(String vaNumber) {
        Code128Writer writer = new Code128Writer();
        // 编码内容, 编码类型, 宽度, 高度, 设置参数
        BitMatrix bitMatrix = writer.encode(vaNumber, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }

    /**
     * 把带logo的二维码下面加上文字
     *
     * @param image   条形码图片
     * @return 返回BufferedImage
     * @author myc
     */
    public static BufferedImage insertWords(BufferedImage image) {
        // 新的图片，把带logo的二维码下面加上文字
            BufferedImage outImage = new BufferedImage(WIDTH, WORDHEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = outImage.createGraphics();
            // 设置白色
            setColorWhite(g2d);
            // 画条形码到新的面板
            g2d.drawImage(image, 10, 40, image.getWidth() - 20, image.getHeight(), null);
            // 画文字到新的面板
            Color color = new Color(0, 0, 0);
            g2d.setColor(color);
            // 字体、字型、字号
            g2d.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            //文字长度
            //左上角文字长度
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String formattedDate = sdf.format(date);
            String printDate = "身份码日期：" +formattedDate;
            int leftUpWordsWidth = WIDTH - g2d.getFontMetrics().stringWidth(printDate);
            // 画文字-上部分
            g2d.drawString(RIGHT_UP_WORDS, 20, 30);
            g2d.drawString(printDate, leftUpWordsWidth - 20, 30);
            g2d.dispose();
            outImage.flush();
            return outImage;
    }


    /**
     * 设置背景为白色
     *
     * @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
     */
    private static void setColorWhite(Graphics2D g2d) {
        // 设置画笔颜色为白色
        g2d.setColor(Color.WHITE);
        // 填充整个屏幕
        g2d.fillRect(0, 0, 603, 603);
        // 设置画笔颜色为黑色
        g2d.setColor(Color.BLACK);

    }

}

